<template>
	<view class="module flex_ZC">
		<!-- #ifdef H5 -->
			<uNavbar title="证件管理"> </uNavbar>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<image class="indexbg" :src="baseUrl +'/static/image/indexbg.png'" mode="aspectFill"></image>
		<view class="custom_box">
			<view :style="{height: statusBarHeight + 'px', backgroundColor: backgroundColor }"></view>
			<view class="custom_nav " :style="{height: titleHeight + 'px', backgroundColor: backgroundColor}">
				<image class="goback" :src="baseUrl +'/static/icon/i_left.png'" mode="aspectFill" @click="leftClick">
				</image>
				<text class="f28 c3">证件管理</text>
			</view>
		</view>
		<view :style="{height: statusBarHeight + titleHeight + 'px'}" style="width: 100%;"></view>
		<!-- #endif -->
		<view class="information_txt flex_Z">
			<text class="f48 c3 fw7">上传身份信息</text>
		</view>

		<view class="content padb30 flex_Z">
			<view class="  flex_Z">
				<view class="flex_dq">
					<text class="f28 c3 fw5">身份证</text>
					<text class="tishi"> *</text>
				</view>
				<text class="content_tip">上传完成后自动识别</text>
			</view>
			<u-upload :fileList="fileList1" @afterRead="afterRead($event,'face',)" @delete="deletePic($event,'face',)"
				name="1" :maxCount="1" :maxSize="6 *1024 *1024" @oversize="overSize" width="300" height="170">
				<view class="content_r flex_c">
					<image :src="baseUrl +'/static/image/sfzzm.png'" mode="aspectFit"></image>
				</view>
			</u-upload>

			<u-upload :fileList="fileList2" @afterRead="afterRead($event,'back',)" @delete="deletePic($event,'back',)"
				name="2" :maxCount="1" :maxSize="6 *1024 *1024" @oversize="overSize" width="300" height="170">
				<view class="content_r mart20 flex_c">
					<image :src="baseUrl +'/static/image/sfzfm.png'" mode="aspectFit"></image>
				</view>
			</u-upload>
		</view>

		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">姓名</text>
				<text class="tishi"> *</text>
			</view>
			<u--input placeholder="请输入姓名" fontSize="14" color="#666666" border="none "
				v-model.trim="ocrIdentityInfo.id_card_name" clearable></u--input>
		</view>
		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">身份证号</text>
				<text class="tishi"> *</text>
			</view>
			<u--input placeholder="请输入身份证号 " fontSize="14" color="#666666" border="none "
				v-model.trim="ocrIdentityInfo.id_number" clearable></u--input>
		</view>
		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">身份证有效期</text>
				<text class="tishi"> *</text>
			</view>
			<view class="padb30 mart20 flex_dq">
				<text class="f28 c6">{{ocrIdentityInfo.id_number_start}}</text>
				<text class="f28 c6 mlr"> 至</text>
				<text class="f28 c6">{{ocrIdentityInfo.id_number_end}}</text>
			</view>
		</view>
		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">所在地址</text>
				<text class="tishi"> *</text>
			</view>
			<u--input placeholder="请输入所在地址" fontSize="14" color="#666666" border="none "
				v-model.trim="ocrIdentityInfo.id_address" clearable></u--input>
		</view>
		<view class="content flex_Z">
			<view class="flex_dq">
				<text class="f28 c3 fw5">法人手机号</text>
				<text class="tishi"> *</text>
			</view>
			<u--input placeholder="请输入法人手机号"  type="number" fontSize="14" maxlength="11" color="#666666" border="none "
				v-model.trim="ocrIdentityInfo.legal_mobile_no" clearable></u--input>
		</view>

		<view class="btns flex_ld" v-if="!modifyState">
			<text class="sign_out" @click="leftClick">上一步</text>
			<text class="service" @click="nextStep">下一步</text>
		</view>

		<view class="btns flex_ld" v-else>
			<text class="service2" @click="confirmModify">确认修改</text>
		</view>
		<view class="empty3"></view>

	</view>
</template>

<script>
	import {
		baseURL
	} from "../../utils/request.js"
	import {
		ocrIdcard,
		settledDetails
	} from "../../api/join.js"
	export default {
		data() {
			return {
				statusBarHeight: 0, //状态栏高度
				titleHeight: 0, // 标题栏高度
				backgroundColor: '',
				baseUrl: this.$imageBaseUrl,

				halfSfzzm: '', //半路经正面
				halfSfzfm: '', //半路经反面
				sideDetail: '', //正反状态
				firstShow: false,
				modifyState: false, //是否通过修改按钮进入
				fileList1: [],
				fileList2: [],
				ocrIdentityInfo: {
					id_card_name: '',
					id_number: '',
					id_number_start: '',
					id_number_end: '',
					id_address:'',
					legal_mobile_no:''
				}, //ocr识别信息

			}
		},
		onReady() {
			// #ifdef MP-WEIXIN 
			uni.getSystemInfo({
				success: res => {
					this.statusBarHeight = res.statusBarHeight
					console.log(this.statusBarHeight);
				}
			})
			const custombar = uni.getMenuButtonBoundingClientRect();
			this.titleHeight = (custombar.top - this.statusBarHeight) * 2 + custombar.height
			console.log(this.titleHeight);
			// #endif

		},
		onLoad(e) {
			console.log(e);
			this.modifyState = e.modify
			this.applicationDetails()
		},
		onShow() {
			if (uni.getStorageSync('ocrIdentityInfo')) {
				this.ocrIdentityInfo = uni.getStorageSync('ocrIdentityInfo')
			}
		},
		methods: {
			leftClick() {
				//获取页面栈的长度
				const canNavBack = getCurrentPages()
				console.log(canNavBack.length);
				// 判断是否刷新了浏览器，刷新了浏览器，页面栈只有当前一个
				if (canNavBack.length > 1) {
					uni.navigateBack({
						delta: 1
					})
				} else {
					uni.switchTab({
						url: '/pages/index/cloudCatIndex'
					})
				}
			},

			// 截取半路经
			getPathAfterCom(url) {
				const parts = url.split('.com');
				return parts.length > 1 ? parts[1] : '';
			},
			// 入驻申请详情
			applicationDetails() {
				settledDetails().then(res => {
					console.log(res);
					if (res.code == 1) {
						if (res.data.seller_auth != null) {
							let kkk = res.data.seller_auth;
							let aaaa = {
								id_card_name: kkk.id_card_name,
								id_number: kkk.id_number,
								id_number_start: kkk.id_number_start,
								id_number_end: kkk.id_number_end,
								id_address:kkk.id_address,
								legal_mobile_no:kkk.legal_mobile_no
							};
							this.firstShow = true
							this.ocrIdentityInfo = aaaa;
							this.fileList1.push({
								'thumb': kkk.id_card_front,
								'status': 'success'
							})
							this.fileList2.push({
								'thumb': kkk.id_card_back,
								'status': 'success'
							})
							this.halfSfzzm = this.getPathAfterCom(kkk.id_card_front)
							this.halfSfzfm = this.getPathAfterCom(kkk.id_card_back)
						}
					}
				})
			},
			// 超出提示
			overSize(event){
				// console.log(event);
				uni.$u.toast('图片过大,请重新上传!')
			},
			// 删除图片
			deletePic(event, side) {
				console.log(event, side);
				this[`fileList${event.name}`].splice(event.index, 1);
				if (side == 'face') {
					this.halfSfzzm = ''
				} else {
					this.halfSfzfm = ''
				}
			},
			// 新增图片
			async afterRead(event, side) {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				this.sideDetail = side
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, );
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen, 1, Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
				uni.hideLoading();
			},
			uploadFilePromise(url) {
				console.log(url);
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseURL + `localSeller/common/upload`,
						filePath: url,
						name: 'file',
						header: {
							"ba-user-token": uni.getStorageSync("token") || ''
						},
						success: (res) => {
							console.log(JSON.parse(res.data))
							let datainfo = JSON.parse(res.data);
							if (datainfo.code == 1) {
								setTimeout(() => {
									resolve(res.data.data);
								}, 1000);
								if (this.sideDetail == 'face') {
									this.firstShow = true
									this.halfSfzzm = JSON.parse(res.data).data.url
								} else {
									this.halfSfzfm = JSON.parse(res.data).data.url
								}
								this.distinguishOcr(JSON.parse(res.data).data.full_url)
							} else {
								uni.$u.toast(datainfo.msg);
								if (this.sideDetail == 'face') {
									this.fileList1 = []
								} else {
									this.fileList2 = []
								}
							}
						}
					});
				});
			},
			// OCR识别
			distinguishOcr(thumb) {
				console.log(thumb);
				console.log('身份证---', this.sideDetail);
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				let data = {
					image: thumb,
					side: this.sideDetail
				};
				ocrIdcard(data).then(res => {
					console.log(res);
					uni.hideLoading();
					if (res.code == 1) {
						if (this.sideDetail == 'face') {
							this.ocrIdentityInfo.id_card_name = res.data.name
							this.ocrIdentityInfo.id_number = res.data.idcard
							this.ocrIdentityInfo.id_address = res.data.address
						} else {
							console.log(2222);
							this.ocrIdentityInfo.id_number_start = res.data.start_date
							this.ocrIdentityInfo.id_number_end = res.data.end_date
						}
					}
				})
			},
			// 规则校验
			ruleValidation() {

				if (this.fileList1.length <= 0) {
					uni.$u.toast('请上传身份证正面');
					return false;
				}
				if (this.fileList2.length <= 0) {
					uni.$u.toast('请上传身份证反面');
					return false;
				}
				if (uni.$u.test.isEmpty(this.ocrIdentityInfo.id_card_name)) {
					uni.$u.toast('请输入法人姓名');
					return false;
				}
				if (uni.$u.test.isEmpty(this.ocrIdentityInfo.id_number)) {
					uni.$u.toast('请输入身份证号');
					return false;
				}
				if (uni.$u.test.isEmpty(this.ocrIdentityInfo.id_address)) {
					uni.$u.toast('请输入所在地址');
					return false;
				}
				if (!uni.$u.test.mobile(this.ocrIdentityInfo.legal_mobile_no)) {
					uni.$u.toast('请输入法人手机号');
					return false;
				}
				return true;
			},

			// 下一步
			nextStep() {
				if (this.ruleValidation()) {
					this.$set(this.ocrIdentityInfo, "id_card_front", this.halfSfzzm);
					this.$set(this.ocrIdentityInfo, "id_card_back", this.halfSfzfm);
					console.log(this.ocrIdentityInfo);
					uni.setStorageSync('ocrIdentityInfo', this.ocrIdentityInfo)
					uni.navigateTo({
						url: '/pagesMy/businessJoin/joinApplyFour'
					})
				}
			},
			// 确认修改
			confirmModify() {
				if (this.ruleValidation()) {
					this.$set(this.ocrIdentityInfo, "id_card_front", this.halfSfzzm);
					this.$set(this.ocrIdentityInfo, "id_card_back", this.halfSfzfm);
					console.log(this.ocrIdentityInfo);
					uni.setStorageSync('ocrIdentityInfo', this.ocrIdentityInfo)
					uni.setStorageSync('sfzShow', true)
					uni.navigateBack({
						delta: 1
					})
				}
			}
		},
		// 页面滚动
		onPageScroll: function(e) {
			const scrollTop = e.scrollTop; // 获取页面滚动位置
			// 根据滚动位置修改状态栏样式
			if (scrollTop > 20) {
				this.backgroundColor = '#ffffff';
			} else {
				this.backgroundColor = '';
			}
			// console.log("滚动距离为：" + e.scrollTop);
		},
	}
</script>

<style scoped>
	.module {
		width: 750rpx;
		margin: 0 auto;
	}

	.custom_box {
		width: 750rpx;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 9;
		padding-bottom: 10rpx;
		opacity: 1;
	}

	.indexbg {
		position: absolute;
		top: 0;
		left: 0;
		width: 750rpx;
		height: 260rpx;
		z-index: 7;
	}

	.custom_nav {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 750rpx;
		font-size: 36rpx;
		z-index: 9;
		color: #FFFFFF;
	}

	.goback {
		position: absolute;
		left: 30rpx;
		text-align: center;
		width: 40rpx;
		height: 40rpx;
	}

	.information_txt {
		width: 686rpx;
		padding: 16rpx 0;
	}


	.content {
		width: 686rpx;
		box-shadow: inset 0rpx -1rpx 0rpx 0rpx rgba(0, 0, 0, 0.05);
		margin: 30rpx 0 10rpx;
	}

	.content_tip {
		font-size: 20rpx;
		color: #999999;
		margin: 10rpx 0 20rpx;
	}

	.content_r {
		width: 690rpx;
		height: 390rpx;
		/* border: 1rpx solid rgba(0, 0, 0, 0.1); */
		border-radius: 20rpx;
		margin: 0 auto;
	}

	.content_r>image {
		width: 600rpx;
		height: 360rpx;
	}

	.mart20 {
		margin-top: 20rpx;
	}

	.padb30 {
		padding-bottom: 30rpx;
	}

	.tishi {
		font-size: 28rpx;
		color: #FA6E01;
		margin-left: 6rpx;
	}

	.mlr {
		margin: 0 10rpx;
	}


	.btns {
		position: fixed;
		bottom: 0rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 690rpx;
		height: 150rpx;
		background: #f8f8f8;
		z-index: 999;
	}

	.sign_out {
		width: 158rpx;
		height: 88rpx;
		font-size: 32rpx;
		color: #333333;
		line-height: 88rpx;
		text-align: center;
		background: #FFFFFF;
		font-weight: 500;
		border-radius: 20rpx;
		border: 2rpx solid rgba(153, 153, 153, 0.18);
	}

	.service {
		width: 504rpx;
		height: 88rpx;
		line-height: 88rpx;
		font-size: 32rpx;
		color: #333333;
		font-weight: 500;
		text-align: center;
		background: #FFCC28;
		border-radius: 20rpx;
	}

	.service2 {
		width: 690rpx;
		height: 88rpx;
		line-height: 88rpx;
		font-size: 32rpx;
		color: #333333;
		font-weight: 500;
		text-align: center;
		background: #FFCC28;
		border-radius: 20rpx;
	}
	/deep/.u-upload__wrap{
		margin: 0 auto !important;
	}
	/deep/.u-input__content {
		margin-left: -20rpx;
	}

	/deep/.u-upload__deletable {
		height: 24px !important;
		width: 24px !important;
	}

	/deep/.u-icon__icon {
		font-size: 30rpx !important;
		line-height: 30rpx !important;
	}
	/* #ifdef MP-ALIPAY */
	/deep/.u-input__content__field-wrapper__field{
		background: transparent!important;
	}
	/* #endif */
</style>